package com.example.case06_729;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodModel implements FoodIface{
    private Retrofit retrofit;
    public FoodModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.130:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }


    @Override
    public void getResultList(int shop_id, final FoodListener listener) {
        FoodService service = retrofit.create(FoodService.class);
        Call<List<DemoBean>> call = service.getFoodByShop(shop_id);

        call.enqueue(new Callback<List<DemoBean>>() {
            @Override
            public void onResponse(Call<List<DemoBean>> call, Response<List<DemoBean>> response) {
                if(response.isSuccessful() && response.body()!=null)
                    listener.onResponse(response.body());
                else listener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<DemoBean>> call, Throwable t) {
                listener.onFail(t.toString());
                System.out.println("--DemoModel--"+t.toString());
            }
        });
    }

}
